/*

pages.product.detail.js

Author: Byron Salau
Contributors:

This file is part of Blanc

Licenced under the MIT License (MIT)
 
Copyright (C) 2011 - 2012 Codagenic Pty. Ltd

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

*/

global.ENV = 
{
	FORCE_JQUERY: false
};


/**
* Products Detail Methods
* @namespace
*/
Blanc.Pages.Products.Detail = 
{
	
	// Properties
	$addedModal: null,

	$addedWishlistModal: null,

	// Namespace for our product ember application
	Em: {},

  /**
	* Constructor
	*/
	init: function ()
	{

		this.$addedModal = $('#AddedModel').jqm();
		this.$addedWishlistModal = $('#AddedModelToWishlist').jqm();

		Blanc.Pages.Products.Detail.setTabs();

		this.createApplication();
	},

	/**
	*  Create Our Ember Application
	*/
	createApplication: function ()
	{
		var self = this,
			namespace = "Product",
			App = Blanc.App(namespace);

		App.addModels(Blanc.Pages.Products.Detail.Em.models(namespace));
		App.addControllers(Blanc.Pages.Products.Detail.Em.controllers(namespace));
		App.addViews(Blanc.Pages.Products.Detail.Em.Views(namespace));
		App.addViews(Blanc.Controls.CartStatusBtn.Em.Views(namespace));

		//Create & Append views
		App.ready = function ()
		{
			App.Views.AttributeListView.create().appendTo('#emberAttributeListViewRoot');
			App.Views.ProductPriceView.create().appendTo('#emberProductPriceViewRoot');
			App.Views.SelectedVarationView.create().appendTo('#emberSelectedVarationViewRoot-Cart');
			App.Views.SelectedVarationView.create().appendTo('#emberSelectedVarationViewRoot-Wishlist');
			App.Views.MiniCartView.create().appendTo('#emberMiniCartViewRoot');
		};

		// Assets Event Handling
		App.Controllers.ProductController.on('load assetsChange', function (e)
		{
			var assets = App.Controllers.ProductController.assets();
			self.AssetController.load(assets);
		});

	},

	/**
	* Product Added to cart 
	* Called when items are added to the cart
	*/
	didAddItemToCart: function (data)
	{

		var $count = $('#cartStatus-count'),
			$value = $('#cartStatus-total > span'),
			$cart = $('#cart-list-compact'),
			$cartBtn = $('#cartLink');

		if (Codagenic.isString(data))
		{
			// There was a problem (eg out of stock)
			alert("Item currently cannot be added to cart \n\n Reason:'" + data + "'");
			return;
		}

		// Update the UI
		if (!$count.length)
		{
			$value.parent().after("<span id=\"cartStatus-count\" class=\"items-count\">" + data.totalItems + "</span>");
		}
		else
		{
			$count.text(data.totalItems);
		}

		// Update the data attribute
		$cartBtn.attr("data-items", data.totalItems);

		//Update Text Value
		$value.text("(" + data.formattedValueString + ")");

		// Remove the cart items, ember will re-render
		$cart.remove();

		// Show the success modal window
		this.$addedModal.jqmShow();

	},

	/**
	* Product Added to wishlist 
	* Called when items are added to the wishlist
	*/
	didAddItemToWishlist: function (totalItems)
	{
		// Update UI
		if (totalItems)
		{
			$('#wlItemsCount').text("(" + totalItems + ")");
		}

		// Show the modal
		this.$addedWishlistModal.jqmShow();

	},

	/**
	* Tabs 
	*/
	setTabs: function () 
	{

		var $tabContainers = $('#productTabs > div'),
			$tabNavigationItems = $('#productTabs ul.tab-navigation a');
		
		$tabNavigationItems.on('click', function (e) 
		{
			var $target = $tabContainers.filter(this.hash);

			$tabContainers.hide();

			if ($(window).width() < 767) 
			{
				$target
					.detach()
					.appendTo($('body'))
					.css({top: $(window).height()})
					.show()
					.animate({
						top: 0
					}, 500);
			} 
			else
			{
				$target.show();
			}

			$('#productTabs ul.tab-navigation a').removeClass('selected');
			$(this).addClass('selected');
			e.preventDefault();
		});

		$('#productTabs .tab-toolbar').on('click', 'span', function (e) 
		{
			$(this).parent().parent().animate(
			{
				top: $(window).height()
			}, 400, function () 
			{
				return $(this).detach().appendTo($('#productTabs'));
			});
		});

		if ($(window).width() > 767) 
		{
			$tabNavigationItems.filter(':first').click();
		}
	},

	/*
	* Asset Controller
	* Methods for handling the asset view port
	*/
	AssetController:
	{

		// Singleton Instance
		_instance: null,

		// asset Property
		assets: null,

		// Constructor
		init: function (data)
		{
			if (Codagenic.isDefined(Galleria) && !this._instance)
			{
				Galleria.loadTheme(Blanc.Config.galleriaThemePath);
				Galleria.run('#media', { dataSource: data });
				this._instance = $('#media').data('galleria');
			}
		},

		// Update/init with new assets
		load: function (obj)
		{
			if (this._instance == null)
			{
				this.init(obj);
			}
			else
			{
				if (Codagenic.isEqual(this.assets, obj))
				{
					// Bail out if the variation images are the same
					return;
				}

				this._instance.load(obj);
			}

			this.assets = obj;
		}
	}
};